package com.mengfou.appserver.app;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Intent;
import android.os.Build;
import android.os.IBinder;
import android.util.Log;

import androidx.annotation.Nullable;
import androidx.core.app.NotificationCompat;

import com.mengfou.appserver.R;
import com.mengfou.appserver.ServerManager;

/**
 * @author 梦否 on 2024/1/5
 * @blog https://mengfou.blog.csdn.net/
 */
public class AppService extends Service {

    private ServerManager serverManager;
    private static final String channelId = "service";
    private static final String TAG = AppService.class.getName();
    private static final int SERVICE_ID = 10;

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        serverManager = new ServerManager();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        serverManager.start();
        NotificationManager notificationManager = getSystemService(NotificationManager.class);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel(channelId, "bg", NotificationManager.IMPORTANCE_HIGH);
            channel.enableLights(true);
            channel.enableVibration(true);
            notificationManager.createNotificationChannel(channel);

            Notification notification = new NotificationCompat.Builder(this, channelId)
                    .setContentTitle("AppService")
                    .setContentText("服务运行中...")
                    .setSmallIcon(R.drawable.ic_launcher_background)
                    .build();

            startForeground(SERVICE_ID, notification);
            return super.onStartCommand(intent, flags, startId);
        }
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        serverManager.stop();
        super.onDestroy();
    }
}
